<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GENERATOR" CONTENT="Microsoft FrontPage 4.0">
   <TITLE>color conversions</TITLE>
   <link rel="stylesheet" type="text/css" href="../../avisynth.css">
</HEAD>
<BODY>
<p>work under construction.</p>
<p>Should cover RGB->YUV conversions and lumarange scaling/preservation and when to use which conversion.</p>
<h2>Color conversions</h2>
<table border="1" width="75%">
  <tr>
    <td width="33%"> coefficients</td>
    <td width="33%" align="center"> Rec.601</td>
    <td width="34%" align="center"> Rec.709&nbsp;</td>
    <td width="34%" align="center"> FCC</td>
  </tr>
  <tr>
    <td width="33%"> Kr : Red channel coefficient</td>
    <td width="33%" align="center"> 0.299</td>
    <td width="34%" align="center"> 0.2125</td>
    <td width="34%" align="center"> 0.3</td>
  </tr>
  <tr>
    <td width="33%"> Kg : Green channel coefficient</td>
    <td width="33%" align="center"> 0.587</td>
    <td width="34%" align="center"> 0.7154</td>
    <td width="34%" align="center"> 0.59</td>
  </tr>
  <tr>
    <td width="33%"> Kb : Blue channel coefficient</td>
    <td width="33%" align="center"> 0.114</td>
    <td width="34%" align="center"> 0.0721</td>
    <td width="34%" align="center"> 0.11</td>
  </tr>
</table>
<p>(0.0 &lt;= [Y,R,G,B] &lt;= 1.0) ; (-1.0 &lt; [U,V] &lt; 1.0)</p>
<p>Kg = 1 - Kr - Kb</p>
<p>Y = Kr*R + Kg*G + Kb*B<br>
V = (R - Y)/(1-Kr) = R - G * Kg/(1-Kr) - B * Kb/(1-Kr)<br>
U = (B - Y)/(1-Kb) = - R * Kr/(1-Kb) - G * Kg/(1-Kb) + B</p>
<p>R = Y + V*(1-Kr)<br>
G = Y - U*(1-Kb)*Kb/Kg - V*(1-Kr)*Kr/Kg<br>
B = Y + U*(1-Kb)</p>
<h3>Converting to programming values</h3>
<p><b>YUV [0,255] &lt;-> RGB [0,255]</b> (0 &lt;= [r,g,b] &lt;= 255, 0 &lt;= y &lt;= 255, 0 &lt; [u,v] &lt; 255)</p>
<p>y = Y * 255<br>
v = V * 127.5 + 128<br>
u = U * 127.5 + 128<br>
r = R * 255<br>
g = G * 255<br>
b = B * 255</p>
<p>Substituting (Y,V,U,R,G,B) in the equations above and multiplying with 127.5 and respectively 255 gives</p>
<p>y = Kr*r + Kg*g + Kb*b<br>
v - 128 = 0.5*(r - y)/(1-Kr) = 0.5 * r - 0.5 * g * Kg/(1-Kr) - 0.5 * b * Kb/(1-Kr)<br>
u - 128 = 0.5*(b - y)/(1-Kb) = - 0.5 * r * Kr/(1-Kb) - 0.5 * g * Kg/(1-Kb) + 0.5 * b</p>
<p>r = y + 2*(v-128)*(1-Kr)<br>
g = y - 2*(u-128)*(1-Kb)*Kb/Kg - 2*(v-128)*(1-Kr)*Kr/Kg<br>
b = y + 2*(u-128)*(1-Kb)</p>
<p><b>YUV [16,235] &lt;-> RGB [0,255]</b> (0 &lt;= [r,g,b] &lt;= 255, 16 &lt;= y &lt;= 235, 16 &lt;= [u,v] &lt;= 240)</p>
<p>y = Y * 219 +  16<br>
u = U * 224 + 128<br>
v = V * 224 + 128<br>
r = R * 255<br>
g = G * 255<br>
b = B * 255</p>
<h2>References</h2>
<p>http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html<br>
ITU BT.601 ...<br>
ITU BT.709 ...</p>
<p><kbd>$Date: 2005/10/03 16:31:31 $</kbd></p>
<FORM><INPUT TYPE="Button" VALUE="Back"
onClick="history.go(-1)"></FORM>
</BODY>
</HTML>
